package com.senac.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.StringUtils;
import com.senac.dao.ProfessorDao;
import com.senac.exception.DisciplinaException;
import com.senac.exception.DisciplinaException.DISC_ERROR;
import com.senac.model.Disciplina;
import com.senac.model.Professor;
import com.senac.service.DisciplinaService;
import com.senac.service.DisciplinaServiceLocal;

@WebServlet("/disciplina")
public class CrudDisciplinaServlet extends GerenciamentoEscolaServlet<Disciplina> {

	private static final long serialVersionUID = -1250441818738201346L;
	
	
		
	protected void cadastrar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		ProfessorDao profDao = new  ProfessorDao();
		List<Professor> professores =  profDao.getProfessores();
		
		req.setAttribute("professores", professores);
		req.getRequestDispatcher("pages/disciplinaView.jsp").forward(req, resp);
	}
	
	protected void salvar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		
		String idString = req.getParameter("id");
		
		try {
			
			DisciplinaServiceLocal service = new DisciplinaService();
			Disciplina disciplina = mountByRequest(req);
			boolean ok = service.save(disciplina);
			
			if(ok) {
				resp.sendRedirect("listarDisciplinas?msg=salvo");
			} else {
				resp.sendRedirect("listarDisciplinas?msg=erro_salvar");
			}		
			
		} catch (DisciplinaException e) {
			
			DISC_ERROR error = e.getError();
			
			if(StringUtils.isEmptyOrWhitespaceOnly(idString)) {
				req.getRequestDispatcher("disciplina?acao=&msg="+error.toString().toLowerCase()).forward(req, resp);
			} else {
				req.getRequestDispatcher("disciplina?acao=editar&id="+idString+"&msg="+error.toString().toLowerCase()).forward(req, resp);
			}
			
		} catch (Exception e) {
			resp.sendRedirect("listarDisciplinas?msg=erro_salvar");
		}
		
	}
	
	
	protected void editar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

		DisciplinaServiceLocal service = new DisciplinaService();
		ProfessorDao profDao = new  ProfessorDao();
		
		String idString = req.getParameter("id");
		Integer id = Integer.parseInt(idString);

		Disciplina disciplina = service.getDisciplinaById(id);
		List<Professor> professores =  profDao.getProfessores();
		
		req.setAttribute("disciplina", disciplina);
		req.setAttribute("professores", professores);
		
		String url = "pages/disciplinaView.jsp?" + req.getQueryString();
		req.getRequestDispatcher(url).forward(req, resp);

	}
	
	
	
	protected void excluir(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

		DisciplinaServiceLocal service = new DisciplinaService();
		String idString = req.getParameter("id");
		Integer id = Integer.parseInt(idString);
		
		
		
		boolean ok = service.delete(id);

		if (ok) {
			
			resp.sendRedirect("listarDisciplinas?msg=removido");
		} else {
			resp.sendRedirect("listarDisciplinas?msg=erro_remover");
		}
	}
	
	
	
	protected Disciplina mountByRequest(HttpServletRequest req) {
		Disciplina disciplina = new Disciplina();
		String idString = req.getParameter("id");
		String cargaHoraria = req.getParameter("carga_horaria");
		String idProfessor = req.getParameter("idProfessor");
		
		if (!StringUtils.isNullOrEmpty(idString)) 
		{
			disciplina.setId(Integer.parseInt(idString));
		}
		
		disciplina.setNome(req.getParameter("nome"));

		if (!StringUtils.isNullOrEmpty(cargaHoraria)) 
		{
			disciplina.setCargaHoraria(Integer.parseInt(cargaHoraria));
		}
		
		if (!StringUtils.isNullOrEmpty(idProfessor)) 
		{
			Professor professor = new Professor();
			professor.setId(Integer.parseInt(idProfessor));
			disciplina.setProfessor(professor);
		}
		
		disciplina.setConteudo(req.getParameter("conteudo").trim());
		
		return disciplina;
	}
	
	protected void listar(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		
		DisciplinaService service = new DisciplinaService();
		List<Disciplina> list = service.getDisciplinas();
		System.out.println("listar"+list.size());
		req.setAttribute("disciplinas", list);
		req.getRequestDispatcher("pages/disciplinaListar.jsp").forward(req, resp);

	}
	
	

}
